import execjs
import json
import os
import sys

Path_Project = sys.path[0]
Path_Project = Path_Project.replace('\commands' , '\\build\\wechatgame\\engine\\src\\')
print("Path_Project " + Path_Project)

def merge(file1, file2):
    f1 = open(file1, 'a+', encoding='utf-8')
    with open(file2, 'r', encoding='utf-8') as f2:
        f1.write('\n')
        for i in f2:
            f1.write(i)

tempjs = Path_Project + 'settings_temp.js'
jsfile = Path_Project + 'settings.js'
jsonfile = Path_Project + 'settings.json'
zipfile = Path_Project + "settings.zip"

with open(tempjs, 'w') as f:
    f.write("window = {};\n")

merge(tempjs, jsfile)

with open(tempjs, 'r', encoding='UTF-8') as f:
    js_code = f.read()
context = execjs.compile(js_code)
result = context.eval("window._CCSettings")

with open(jsonfile, 'w') as write_f:
    json.dump(result, write_f, separators=(',', ':'))

os.system('7z a ' + zipfile + " " + jsonfile)

os.remove(tempjs)
os.remove(jsfile)
os.remove(jsonfile)

